<?php
/**
 * Created by PhpStorm.
 * User: azhuang
 * Date: 2018/4/12
 * Time: 22:11
 */

namespace Admin\Controller;


use Admin\Model\DeptModel;
use function PHPSTORM_META\elementType;

class DeptController extends BaseController
{
	public function shilihua()
	{
//		$model = new DeptModel();
//		dump($model);
		$model = D('Dept');
		$model = D();

		$model = M('Dept');
		$model = M();
		dump($model);

	}

	public function tianjia()
	{
		$model = M('Dept');
		$data = array('name' => '财务部', 'pid' => '0',
			'sort' => '2', 'remark' => '这是财务部门'
		);
		$result = $model->add($data);
		echo $result;
		dump($result);
	}

	public function addAll()
	{
		$model = M('Dept');
		$data = array(
			array('name' => '总裁办', 'pid' => '0',
				'sort' => '4', 'remark' => '权利最高的部门'
			),
			array('name' => '公关部', 'pid' => '0',
				'sort' => '3', 'remark' => '这是公关部'
			),
		);
		foreach ($data as $datum) {
			$result = $model->add($datum);
			dump($result);
		}
	}

	public function xiugai()
	{
		$model = M('Dept');
		$data = array('sort' => '22', 'remark' => '今天发工资',
		);
		$data = array('id' => '2', 'sort' => '22', 'remark' => '今天发工资',
		);
		$result = $model->save($data);
		dump($result);
	}

	public function chaxun()
	{
		$model = M('Dept');
		$data = $model->select();
		$data = $model->select('1,2,3');
		$data = $model->select('1');
		$data = $model->select(2);
		dump($data);

	}

	public function shanchu()
	{
		$model = M('Dept');
		$model->delete();
		$result = $model->delete(7);
		dump($result);
	}

	public function add()
	{
//		判断是否是post提交过来的
		if (IS_POST) {
//			$post = I('post.');
			$model = D('Dept');
//			$result = $model->add($post);
			$data = $model->create();
//			echo $data;
			if (!$data) {
//				用户验证失败
//				echo $model->getError();
				$this->error($model->getError());
				die;
			}
//			dump($data);die;
			$result = $model->add();
			if ($result) {
				$this->success('添加成功', U('showList', 3));
			} else {
				$this->error('添加失败');
			}
		} else {
//获取顶级部门
			$model = M('Dept');
//			$data = $model->where('pid = 0')->select();
			$data = $model->select();
//			dump($data);
			$this->assign('data', $data);
			$this->display();
		}
	}

	public function showList()
	{
		$model = M('Dept');
		$result = $model->order('sort asc')->select();

		foreach ($result as $key => $value) {
//			判断pid
			if ($value['pid'] > 0) {
//				查pid对应的上级部门名字
				//查询pid对应的部门信息
				$info = $model->find($value['pid']);
				//只需要保留其中的name
				$result[$key]['deptname'] = $info['name'];
			}
		}

		load('@/tree');
//		var_dump($result);die;
		$result = getTree($result);

		$this->assign('mDatas', $result);
//		var_dump($result);die;
		$this->display();
	}

	public function edit()
	{
		if (IS_POST) {
//修改确定后的操作
			$post = I('post.');
			$model = D('Dept');
//			$result = $model->add($post);
			$saveResult = $model->create();
			if (!$saveResult) {
//				用户验证失败,用户是否提交完整的信息
//				echo $model->getError();
				$this->error($model->getError());
				die;
			}
			$result = $model->save($post);
			if ($result) {
				$this->success('编辑成功', U('showList', 3));
			} else {
				$this->error('编辑失败');
			}

		} else {
//b编辑后的操作
			$get = I('get.');
			$model = M('Dept');
			$mdatas = $model->order('sort asc')->select();
			$info = $model->find($get['id']);
//		dump($info) ;die;
			$this->assign('mdatas', $mdatas);
			$this->assign('info', $info);
			$this->display();
		}
	}

	public function delete()
	{
		$get = I('get.');
//		dump($get);die;
		$model = D('Dept');
		$result = $model->delete($get['id']);
//		dump($result);die;
		if ($result) {
			$this->success('删除成功', U('showList', 3));
		} else {
			$this->error('删除失败');
		}
	}
}